package cloud.shyidong.core.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Description TODO
 * @Author syd
 * @Date 2023/3/27
 */
@Component
public final class SpringUtils implements ApplicationContextAware {
    
    private static ApplicationContext applicationContext;
    
    public static String getProperty(String key) {
        return applicationContext.getEnvironment().getProperty(key);
    }
    
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
    
    public static <T> T getBean(String name, Class<T> clazz) {
        return applicationContext.getBean(name, clazz);
    }
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }
    
}
